package com.imooc.service.impl;

import com.imooc.common.dto.CartDTO;
import com.imooc.common.enums.ProductStatusEnum;
import com.imooc.common.enums.ResultEnum;
import com.imooc.common.exception.SelllException;
import com.imooc.dataobject.ProductInfo;
import com.imooc.repository.ProductInfoRepository;
import com.imooc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Objects;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductInfoRepository repository;

    @Override
    public ProductInfo findOne(String productId) {
        return repository.findById(productId).orElse(null);
    }

    @Override
    public List<ProductInfo> findUpAll() {
        return repository.findByProductStatus(ProductStatusEnum.UP.getCode());
    }

    @Override
    public Page<ProductInfo> findAll(Pageable request) {
        return repository.findAll(request);
    }

    @Override
    public ProductInfo save(ProductInfo record) {
        return repository.save(record);
    }

    @Override
    public void increaseStock(List<CartDTO> cartDtoList) {
        for (CartDTO cartDTO : cartDtoList) {
            ProductInfo productInfo = findOne(cartDTO.getProductId());
            if (Objects.isNull(productInfo)) {
                throw new SelllException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            productInfo.setProductStock(productInfo.getProductStock() + cartDTO.getProductAmount());
            save(productInfo);
        }
    }

    @Override
    @Transactional
    public void decreaseStock(List<CartDTO> cartDtoList) {
        for (CartDTO cartDTO : cartDtoList) {
            ProductInfo productInfo = findOne(cartDTO.getProductId());
            if (Objects.isNull(productInfo)) {
                throw new SelllException(ResultEnum.PRODUCT_NOT_EXIST);
            }

            Integer result = productInfo.getProductStock() - cartDTO.getProductAmount();
            if (result < 0) {
                throw new SelllException(ResultEnum.PRODUCT_STOCK_ERROR);
            }

            productInfo.setProductStock(result);
            save(productInfo);
        }
    }

    @Override
    public ProductInfo onSale(String productId) {
        ProductInfo productInfo = findOne(productId);
        if (Objects.isNull(productInfo)) {
            throw new SelllException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if (ProductStatusEnum.UP.getCode().equals(productInfo.getProductStatus())) {
            throw new SelllException(ResultEnum.PRODUCT_ON_SALE_ERROR);
        }

        productInfo.setProductStatus(ProductStatusEnum.UP.getCode());
        save(productInfo);
        return productInfo;
    }

    @Override
    public ProductInfo offSale(String productId) {
        ProductInfo productInfo = findOne(productId);
        if (Objects.isNull(productInfo)) {
            throw new SelllException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if (ProductStatusEnum.DOWN.getCode().equals(productInfo.getProductStatus())) {
            throw new SelllException(ResultEnum.PRODUCT_ON_SALE_ERROR);
        }

        productInfo.setProductStatus(ProductStatusEnum.DOWN.getCode());
        save(productInfo);
        return productInfo;
    }

}
